<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Add New Student</title>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/css/bootstrap.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/4.6.0/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        .add-form-container {
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 10px;
            background-color: #f9f9f9;
        }
        .add-form-container h2 {
            margin-bottom: 30px;
        }
        .password-container {
            position: relative;
        }
        .password-container input {
            padding-right: 40px;
        }
        .password-container .toggle-password {
            position: absolute;
            right: 10px;
            top: 70%;
            transform: translateY(-50%);
            cursor: pointer;
        }
        .error-message {
            color: red;
            font-size: 0.875em;
            display: none;
        }
    </style>
    <script>
        function confirmAdd() {
            var isValid = true;
            var fields = document.querySelectorAll('.form-control');
            fields.forEach(function(field) {
                if (field.value.trim() === '') {
                    isValid = false;
                    field.nextElementSibling.style.display = 'block';
                } else {
                    field.nextElementSibling.style.display = 'none';
                }
            });

            if (isValid && confirm('是否确实要添加此信息？')) {
                document.getElementById('addForm').submit();
            }
        }

        function togglePassword() {
            var passwordField = document.getElementById('password');
            var toggleIcon = document.getElementById('togglePasswordIcon');
            if (passwordField.type === 'password') {
                passwordField.type = 'text';
                toggleIcon.classList.remove('fa-eye');
                toggleIcon.classList.add('fa-eye-slash');
            } else {
                passwordField.type = 'password';
                toggleIcon.classList.remove('fa-eye-slash');
                toggleIcon.classList.add('fa-eye');
            }
        }

        function showResult() {
            var num = '<%= session.getAttribute("num") %>';
            var message = '<%= session.getAttribute("message") %>';
            if (num === '1') {
                alert('添加成功！');
            } else if (num !== 'null' && num !== ''||num==0) {
                alert(message);
            }
            else {
                alert('添加失败，请检查输入是否正确。');
            }

            // 移除 attribute 以防止重复弹窗
            <% session.removeAttribute("num"); %>
        }

        window.onload = function() {
            var urlParams = new URLSearchParams(window.location.search);
            if (urlParams.has('submitted')) {
                showResult();
            }
        };

        document.addEventListener('DOMContentLoaded', function() {
            var fields = document.querySelectorAll('.form-control');
            fields.forEach(function(field) {
                field.addEventListener('blur', function() {
                    if (field.value.trim() === '') {
                        field.nextElementSibling.style.display = 'block';
                    } else {
                        field.nextElementSibling.style.display = 'none';
                    }
                });
            });
        });
    </script>
</head>
<body>
<div class="container">
    <div class="add-form-container">
        <h2 class="text-center" style="color: blue;"><span>添加新学生</span></h2>
        <form id="addForm" action="${pageContext.request.contextPath}/AddStudent" method="post" accept-charset="UTF-8">
            <div class="form-group">
                <label for="id">ID:</label>
                <input type="number" class="form-control" id="id" name="id" required>
                <div class="error-message">请输入ID</div>
            </div>
            <div class="form-group">
                <label for="name">姓名:</label>
                <input type="text" class="form-control" id="name" name="name" required>
                <div class="error-message">请输入姓名</div>
            </div>
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" class="form-control" id="username" name="username" required>
                <div class="error-message">请输入用户名</div>
            </div>
            <div class="form-group password-container">
                <label for="password">密码:</label>
                <input type="password" class="form-control" id="password" name="password" required>
                <span class="toggle-password" onclick="togglePassword()">
                    <i id="togglePasswordIcon" class="fa fa-eye-slash"></i>
                </span>
                <div class="error-message">请输入密码</div>
            </div>
            <div class="form-group">
                <label for="age">年龄:</label>
                <input type="number" class="form-control" id="age" name="age" required>
                <div class="error-message">请输入年龄</div>
            </div>
            <div class="form-group">
                <label for="gender">性别:</label>
                <select class="form-control" id="gender" name="gender" required>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
                <div class="error-message">请选择性别</div>
            </div>
            <div class="form-group">
                <label for="major">专业:</label>
                <input type="text" class="form-control" id="major" name="major" required>
                <div class="error-message">请输入专业</div>
            </div>
            <button type="button" class="btn btn-primary btn-block" onclick="confirmAdd()">保存</button>
            <a href="student.jsp" class="btn btn-secondary btn-block">取消</a>
        </form>
    </div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/js/all.min.js"></script>
</body>
</html>
